#include "scenes/SceneMainMenu.h"

#include "Dialogs.h"

SceneMainMenu::SceneMainMenu( GameApplication& theApp )
   : App( theApp ),
     currentDialog( NULL )
{
	// None
}

void SceneMainMenu::Initialize()
{
	splash.SetImage( *App.Images.GetResource( App.GameResources[ "splash main menu" ] ) );

	gdn::Image* mediumButtonImage = App.Images.GetResource( App.GameResources[ "gui button medium" ] );

	gdn::Font* font = App.Fonts.GetResource( App.GameResources[ "font sansation" ] );

	playButton = gui::Button( gdn::Vector2f( 17 + ( 64 * 0 ) + ( 10 * 0 ), 195 ),
				              "Play", "Play", "Play",
				              gdn::Color( 0, 0, 0 ), gdn::Color( 0, 0, 0 ), gdn::Color( 255, 255, 255 ),
				              *mediumButtonImage, *font, 19,
				              gdn::Rectanglei( 0, 0, 64, 32 ), gdn::Rectanglei( 64, 0, 64, 32 ), gdn::Rectanglei( 128, 0, 64, 32 ) );

    helpButton = gui::Button( gdn::Vector2f( 17 + ( 64 * 1 ) + ( 10 * 1 ), 195 ),
				              "Help", "Help", "Help",
				              gdn::Color( 0, 0, 0 ), gdn::Color( 0, 0, 0 ), gdn::Color( 255, 255, 255 ),
				              *mediumButtonImage, *font, 19,
				              gdn::Rectanglei( 0, 0, 64, 32 ), gdn::Rectanglei( 64, 0, 64, 32 ), gdn::Rectanglei( 128, 0, 64, 32 ) );

    configButton = gui::Button( gdn::Vector2f( 17 + ( 64 * 2 ) + ( 10 * 2 ), 195 ),
				                "Config", "Config", "Config",
				                gdn::Color( 0, 0, 0 ), gdn::Color( 0, 0, 0 ), gdn::Color( 255, 255, 255 ),
				                *mediumButtonImage, *font, 19,
				                gdn::Rectanglei( 0, 0, 64, 32 ), gdn::Rectanglei( 64, 0, 64, 32 ), gdn::Rectanglei( 128, 0, 64, 32 ) );

    quitButton = gui::Button( gdn::Vector2f( 17 + ( 64 * 3 ) + ( 10 * 3 ), 195 ),
				              "Quit", "Quit", "Quit",
				              gdn::Color( 0, 0, 0 ), gdn::Color( 0, 0, 0 ), gdn::Color( 255, 255, 255 ),
				              *mediumButtonImage, *font, 19,
				              gdn::Rectanglei( 0, 0, 64, 32 ), gdn::Rectanglei( 64, 0, 64, 32 ), gdn::Rectanglei( 128, 0, 64, 32 ) );

	dialogTransparency.SetImage( *App.Images.GetResource( App.GameResources[ "gui dialog transparency" ] ) );
}

void SceneMainMenu::Terminate()
{
	CloseDialog();
}

void SceneMainMenu::Step( gdn::Window& window )
{
	gdn::Event Event;
	while ( window.GetEvent( Event ) )
	{
		if ( Event.type == gdn::Event::WindowClosed )
		{
			App.Close();
		}

		if ( !currentDialog )
		{
			if ( playButton.Process( Event ) == GUI_BUTTON_RELEASE )
			{
				OpenDialog< DialogFileSelect >();
			}

			if ( helpButton.Process( Event ) == GUI_BUTTON_RELEASE )
			{
				OpenDialog< DialogHelp >();
			}

			if ( configButton.Process( Event ) == GUI_BUTTON_RELEASE )
			{
				OpenDialog< DialogOptions >();
			}

			if ( quitButton.Process( Event ) == GUI_BUTTON_RELEASE )
			{
				App.Close();
			}
		}
		else
		{
			if ( Event.type == gdn::Event::MouseMoved )
			{
				Event.x -= 27;
				Event.y -= 20;
			}
			else if ( Event.type == gdn::Event::WindowResized )
			{
				Event.width -= 27 * 2;
				Event.height -= 20 * 2;
			}
			currentDialog->Step( Event );
		}
	}
}

void SceneMainMenu::Draw( gdn::Window& window )
{
	window.Clear();


	window.Draw( splash );

	playButton.Draw( window );
	helpButton.Draw( window );
	configButton.Draw( window );
	quitButton.Draw( window );

	gdn::Sprite dialogSprite;
	gdn::RenderImage dialogImage;

	if ( IsDialogOpen() )
	{
		dialogImage.Create( gdn::Vector2i( 268, 200 ) );

		window.Draw( dialogTransparency );
		currentDialog->Draw( dialogImage );
		dialogSprite.SetImage( dialogImage.GetImage() );
		dialogSprite.SetPosition( 27, 20 );
		window.Draw( dialogSprite );
	}

	window.Display();
}

bool SceneMainMenu::IsDialogOpen()
{
	return currentDialog;
}

void SceneMainMenu::CloseDialog()
{
	if ( IsDialogOpen() )
	{
		currentDialog->Terminate();
		delete currentDialog;
		currentDialog = NULL;
	}
}
